package com.dengyafei.controller;

import com.dengyafei.annotation.JwtToken;
import com.dengyafei.bean.SharesEntity;
import com.dengyafei.constant.RecommendConstant;
import com.dengyafei.constant.ResultMessage;
import com.dengyafei.domin.vo.SharesAddVO;
import com.dengyafei.domin.vo.SharesDeleteVO;
import com.dengyafei.domin.vo.SharesLikesVO;
import com.dengyafei.domin.vo.SharesSearchVO;
import com.dengyafei.result.RestApiResult;
import com.dengyafei.service.SharesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 邓亚非
 */
@Slf4j
@RestController
@RequestMapping(value = "/shares")
public class SharesController {

    @Autowired
    SharesService service;

    /**
     * 查询所有的分享
     *
     * @return
     */
    @JwtToken
    @PostMapping(value = "/queryShares")
    public RestApiResult queryShares() {
        log.info("【分享模块-查询所有分享接口-参数】:[{}]", "无参");
        List<SharesEntity> sharesEntities = service.queryAllShares();
        log.info("【分享模块-查询所有分享接口-成功】:[{}]", ResultMessage.Shares.QUERY_SUCCESS);
        return RestApiResult.ok(sharesEntities);
    }

    /**
     * 条件查询
     *
     * @param sharesSearchVO
     * @return
     */
    @JwtToken
    @PostMapping(value = "/search")
    public RestApiResult search(@RequestBody SharesSearchVO sharesSearchVO) {
        log.info("【分享模块-条件查询接口-参数】sharesSearchVO:[{}]", sharesSearchVO);
        List<SharesEntity> sharesEntities = service.searchByConditon(sharesSearchVO);
        log.info("【分享模块-条件查询接口-成功】sharesSearchVO:[{}]", ResultMessage.Shares.QUERY_SUCCESS);
        return RestApiResult.ok(sharesEntities);
    }

    /**
     * 点赞
     *
     * @param sharesLikesVO
     * @return
     */
    @JwtToken
    @PostMapping(value = "/like")
    public RestApiResult like(@RequestBody SharesLikesVO sharesLikesVO) {
        log.info("【分享模块-点赞接口-参数】sharesLikesVO:[{}]", sharesLikesVO);
        Integer integer = service.addLike(sharesLikesVO);
        if (!integer.equals(RecommendConstant.OPERATION_SUCCESS)) {
            log.error("【分享模块-点赞接口-失败】sharesLikesVO,cause:[{}]", ResultMessage.Shares.LIKE_FAILURE);
            return RestApiResult.error(ResultMessage.Shares.LIKE_FAILURE);
        }
        log.info("【分享模块-点赞接口-成功】sharesLikesVO:[{}]", ResultMessage.Shares.LIKE_SUCCESS);
        return RestApiResult.okMsg(ResultMessage.Shares.LIKE_SUCCESS);
    }

    /**
     * 添加一个分享
     *
     * @param addVO
     * @return
     */
    @JwtToken
    @PostMapping(value = "/share")
    public RestApiResult share(@RequestBody SharesAddVO addVO) {
        log.info("【分享模块-添加分享接口-参数】:[{}]", addVO);
        Integer integer = service.addShares(addVO);
        if (integer.equals(RecommendConstant.ALREADY_SHARES)) {
            log.error("【分享模块-添加分享接口-失败】,cause:[{}]", ResultMessage.Shares.ALREADY_SHARE);
            return RestApiResult.error(ResultMessage.Shares.ALREADY_SHARE);
        }
        if (!integer.equals(RecommendConstant.OPERATION_SUCCESS)) {
            log.error("【分享模块-添加分享接口-失败】,cause:[{}]", ResultMessage.Shares.SHARES_FAILURE);
            return RestApiResult.error(ResultMessage.Shares.SHARES_FAILURE);
        }
        log.info("【分享模块-添加分享接口-成功】:[{}]", ResultMessage.Shares.SHARES_SUCCESS);
        return RestApiResult.okMsg(ResultMessage.Shares.SHARES_SUCCESS);
    }

    /**
     * 删除分享接口
     *
     * @param sharesDeleteVO
     * @return
     */
    @JwtToken
    @PostMapping(value = "/delete")
    public RestApiResult delete(@RequestBody SharesDeleteVO sharesDeleteVO) {
        log.info("【分享模块-删除分享接口-参数】:[{}]", sharesDeleteVO);
        Integer integer = service.deleteShare(sharesDeleteVO);
        if (!integer.equals(RecommendConstant.OPERATION_SUCCESS)) {
            log.error("【分享模块-删除分享接口-失败】,cause:[{}]", ResultMessage.Shares.DELETE_FAILURE);
            return RestApiResult.error(ResultMessage.Shares.SHARES_FAILURE);
        }
        log.info("【分享模块-删除分享接口-成功】:[{}]", ResultMessage.Shares.DELETE_SUCCESS);
        return RestApiResult.okMsg(ResultMessage.Shares.DELETE_SUCCESS);
    }

    /**
     * 查询个人分享
     * @return
     */
    @JwtToken
    @PostMapping(value = "/queryByHid")
    public RestApiResult queryByHid(){
        log.info("【分享模块-查询个人分享接口-参数】:[{}]","无参");
        List<SharesEntity> sharesEntities = service.queryByHid();
        log.info("【分享模块-查询个人分享接口-成功】:[{}]",ResultMessage.Shares.QUERY_SUCCESS);
        return RestApiResult.ok(sharesEntities);
    }
}
